package com.good.demo.demos.web;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class GoodController {

    @Cacheable("List<Product>")
    @PostMapping("/goodList")
    public List<Product> goodList(@RequestBody List<String> ids) {

        if (ids.size() > 100){
            throw  new RuntimeException("查询商品数量不能大于100！");
        }
        Map<String,List<Good>> csv= readCsv();
        List<Product> list = new ArrayList<>();
        ids.forEach(id->{
            Product product = getData(id,csv);
            if (!Objects.isNull(product)){
                list.add(product);
            }
        });
        return list;
    }

    private Product getData(String item_id,Map<String,List<Good>> csv){
        if (CollectionUtils.isEmpty(csv.get(item_id))){
            return null;
        }
        List<Good> list = csv.get(item_id);
        Product product = new Product();
        product.setItem_id(list.get(0).getItem_id());
        product.setStatus(list.get(0).getStatus());
        product.setBrand(list.get(0).getBrand());
        product.setTitle(list.get(0).getTitle());

        List<HashMap<String,String>> cats = new ArrayList<>();
        HashMap<String,String> cat = new HashMap<>();
        cat.put("level_1_cat_name",list.get(0).getLevel_1_cat_name());
        cats.add(cat);
        HashMap<String,String> cat2= new HashMap<>();
        cat2.put("level_2_cat_name",list.get(0).getLevel_2_cat_name());
        cats.add(cat2);
        HashMap<String,String> cat3 = new HashMap<>();
        cat3.put("level_3_cat_name",list.get(0).getLevel_3_cat_name());
        cats.add(cat3);
        product.setCats(cats);

        List<Sku> skus = new ArrayList<>();
        List<Attrs> attrsAll = new ArrayList<>();
        // 封装sku
        AtomicReference<BigDecimal> min_price = new AtomicReference<>(BigDecimal.valueOf(Long.parseLong(list.get(0).getSale_price())));
        list.forEach(good -> {
            List<Attrs> attrs = new ArrayList<>();
            if (StringUtils.hasText(good.getAttr_name_1()) || StringUtils.hasText(good.getAttr_value_1())){
                Attrs attr = new Attrs();
                attr.setAttr_key(good.getAttr_name_1());
                attr.setAttr_value(good.getAttr_value_1());
                attrs.add(attr);
                attrsAll.add(attr);
            }
            if (StringUtils.hasText(good.getAttr_name_2()) || StringUtils.hasText(good.getAttr_value_2())){
                Attrs attr2 = new Attrs();
                attr2.setAttr_key(good.getAttr_name_2());
                attr2.setAttr_value(good.getAttr_value_2());
                attrs.add(attr2);
                attrsAll.add(attr2);
            }
            if (StringUtils.hasText(good.getAttr_name_3()) || StringUtils.hasText(good.getAttr_value_3())){
                Attrs attr3 = new Attrs();
                attr3.setAttr_key(good.getAttr_name_3());
                attr3.setAttr_value(good.getAttr_value_3());
                attrs.add(attr3);
                attrsAll.add(attr3);
            }
            Sku sku = new Sku();
            sku.setSku_attrs(attrs);
            sku.setSku_id(good.getSku_id());
            sku.setSale_price(good.getSale_price());
            sku.setStock_num(good.getStock_num());
            skus.add(sku);
            if (min_price.get().compareTo(BigDecimal.valueOf(Long.parseLong(good.getSale_price()))) > 0){
                min_price.set(BigDecimal.valueOf(Long.parseLong(good.getSale_price())));
            }
        });
        product.setMin_price(min_price.get());
        product.setSkus(skus);
        product.setAttrs(attrsAll);
        return product;
    }

    public Map<String,List<Good>> readCsv(){
        final CsvReader reader = CsvUtil.getReader();
        //假设csv文件在classpath目录下
        final List<Good> list = reader.read(ResourceUtil.getUtf8Reader("good.csv"), Good.class);
        log.info("缓存测试解析csv文件{}条",list.size());
        Map<String,List<Good>> map = list.stream().collect(Collectors.groupingBy(Good::getItem_id));
        return map;
    }

    public static void main(String[] args) {
        final CsvReader reader = CsvUtil.getReader();
        //假设csv文件在classpath目录下
        final List<Goods> list = reader.read(ResourceUtil.getUtf8Reader("good.csv"), Goods.class);
        list.forEach(good -> {
            System.out.println(good.getItem_id());
        });

    }
}
